package com.sczmgk.hotline_pzh.ui;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;

import com.sczmgk.hotline_pzh.R;
import com.sczmgk.hotline_pzh.base.BaseActivity;
import com.sczmgk.hotline_pzh.constants.Constants;

import java.util.ArrayList;

/**
 * Created by Administrator on 2017/1/16.
 */
public class SelectAreaActivity extends BaseActivity{

    private String inputArea;
    private GridView mGVArea;
    private ArrayList<String> areaInfos;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_area);

        setHeader();

        initView();

        initData();

        setListener();
    }

    private void initView() {
        mGVArea = (GridView) findViewById(R.id.gv_area_list);
    }

    private void initData() {
        Intent intent = getIntent();
        inputArea = intent.getStringExtra(Constants.SELECT_AREA);

        //TODO 手动添加区域
        areaInfos = new ArrayList<>();
        areaInfos.add(getResources().getString(R.string.area_des)) ;

        MyAdapter adapter = new MyAdapter() ;
        mGVArea.setAdapter(adapter);
    }

    private void setListener() {
        mGVArea.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                String areaDes = areaInfos.get(position);
                Intent intent = new Intent() ;
                intent.putExtra(Constants.SELECT_AREA , areaDes) ;
                setResult(RESULT_OK, intent);
                finish();
            }
        });
    }

    private class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return areaInfos == null ? 0 : areaInfos.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            convertView = View.inflate(SelectAreaActivity.this, R.layout.item_online_complaint_area, null);
            TextView tvArea = (TextView) convertView.findViewById(R.id.tv_area);

            String area = areaInfos.get(position);

            if(areaInfos != null && area.equals(inputArea)){
                tvArea.setTextColor(getResources().getColor(R.color.white));
                tvArea.setBackgroundResource(R.drawable.blue_button_processed_bg);
            }

            tvArea.setText(area);

            return convertView;

        }
    }

    /**
     * 设置头
     */
    private void setHeader() {
        setTitle(R.string.select_area);
        setLeftBtnVisibility(View.VISIBLE);
    }

    @Override
    public void handleMessage(Message msg) {

    }

    @Override
    public boolean getIsNeedLogin() {
        return false;
    }
}
